package com.wzgl.core.webmagic.model;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.wzgl.core.base.Table;
import com.wzgl.core.baseModel.BaseWxGird;
import com.wzgl.core.model.Tdicsort;

/***
 * 格子表
 * @author Administrator
 *
 */
@Table(tableName = "tweixin_grid", primaryKey = "fid")
public class WxTgrid extends BaseWxGird<WxTgrid> {
	private static final long serialVersionUID = -1L;
	public static final WxTgrid dao = new WxTgrid();
	
	/**
	 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
	 */
	public Page<WxTgrid> paginate(int pageNumber, int pageSize,Integer fstatus,String ftitle) {
		String sql= " from tweixin_grid where 1 = 1";
		List<Object> agrs = new ArrayList<Object>();
		if(fstatus != null){
			sql += " and fstatus = ?";
			agrs.add(fstatus);
		}
		if(!StrKit.isBlank(ftitle)){
			sql += "  and factivity_name = ? ";
			agrs.add(ftitle);
		}
		sql += "  order by fdate desc";
		return paginate(pageNumber, pageSize, "select *", sql,agrs.toArray());
	}

	public Page<WxTgrid> paginate(int pageNumber, int pageSize,WxTgrid t) {
		String sql= " from tweixin_grid where 1 = 1";
		List<Object> agrs = new ArrayList<Object>();
		if(t.getFreleaseid() != null){
			sql += " and freleaseid = ?";
			agrs.add(t.getFreleaseid());
		}
		if(!StrKit.isBlank(t.getFactivity_name())){
			sql += "  and factivity_name like '%"+t.getFactivity_name()+"%' ";
		}
		if(!StrKit.isBlank(t.getFtype())){
			sql += "  and ftype = ? ";
			agrs.add(t.getFtype());
		}
		sql += "  order by fdate desc";
		return paginate(pageNumber, pageSize, "select *", sql,agrs.toArray());
	}
	
	public  Page<WxTgrid> findByType(int pageNumber, int pageSize,Integer fstatus,final Integer ftype) {
		String sql= " from tweixin_grid where 1 = 1";
		List<Object> agrs = new ArrayList<Object>();
		if(ftype != null){
			sql += "  and ftype = ? ";
			agrs.add(ftype);
		}
		sql += "  order by fdate desc";
		return paginate(pageNumber, pageSize, "select *", sql,agrs.toArray());
	}
	public WxTgrid baseById(final String fid) {
		return dao.doFindFirst("fid = ?", fid);
	}

	public WxTgrid findById(final String fid) {
//		return dao.getCache(fid, new IDataLoader() {
//			@Override
//			public Object load() {
//				return dao.doFindFirst(" fid = ?", fid);
//			}
//		});
		return dao.doFindFirst(" fid = ?", fid);
	}	
}
